package org.example.lethear.repository;

import org.example.lethear.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 用户数据访问接口
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    
    /**
     * 根据用户名查找用户
     */
    Optional<User> findByUsername(String username);
    
    /**
     * 根据邮箱查找用户
     */
    Optional<User> findByEmail(String email);
    
    /**
     * 根据用户名或邮箱查找用户
     */
    Optional<User> findByUsernameOrEmail(String username, String email);
    
    /**
     * 检查用户名是否存在
     */
    boolean existsByUsername(String username);
    
    /**
     * 检查邮箱是否存在
     */
    boolean existsByEmail(String email);
    
    /**
     * 根据状态查找用户列表
     */
    List<User> findByStatus(Integer status);
    
    /**
     * 根据状态分页查找用户
     */
    Page<User> findByStatus(Integer status, Pageable pageable);
    
    /**
     * 根据邮箱验证状态查找用户
     */
    List<User> findByEmailVerified(Integer emailVerified);
    
    /**
     * 模糊搜索用户（用户名或邮箱）
     */
    @Query("SELECT u FROM User u WHERE u.username LIKE %:keyword% OR u.email LIKE %:keyword%")
    Page<User> searchUsers(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 查找指定时间段内注册的用户
     */
    @Query("SELECT u FROM User u WHERE u.createTime BETWEEN :startTime AND :endTime")
    List<User> findUsersByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, 
                                           @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计用户总数
     */
    @Query("SELECT COUNT(u) FROM User u WHERE u.status = 1")
    long countActiveUsers();
    
    /**
     * 统计今日新增用户数
     */
    @Query("SELECT COUNT(u) FROM User u WHERE u.createTime >= :startOfDay")
    long countNewUsersToday(@Param("startOfDay") LocalDateTime startOfDay);
    
    /**
     * 根据角色查找用户
     */
    @Query("SELECT u FROM User u JOIN u.roles r WHERE r.roleName = :roleName")
    List<User> findByRoleName(@Param("roleName") String roleName);
    
    /**
     * 查找最近活跃的用户（根据更新时间）
     */
    @Query("SELECT u FROM User u WHERE u.status = 1 ORDER BY u.updateTime DESC")
    Page<User> findRecentActiveUsers(Pageable pageable);
    
    /**
     * 预加载角色的分页查询所有用户
     */
    @Query("SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.roles ORDER BY u.createTime DESC")
    Page<User> findAllWithRoles(Pageable pageable);
    
    /**
     * 预加载角色的根据状态分页查询用户
     */
    @Query("SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.roles WHERE u.status = :status ORDER BY u.createTime DESC")
    Page<User> findByStatusWithRoles(@Param("status") Integer status, Pageable pageable);
    
    /**
     * 预加载角色的搜索用户
     */
    @Query("SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.roles WHERE u.username LIKE %:keyword% OR u.email LIKE %:keyword% ORDER BY u.createTime DESC")
    Page<User> searchUsersWithRoles(@Param("keyword") String keyword, Pageable pageable);
    
    /**
     * 根据ID预加载角色查询用户
     */
    @Query("SELECT u FROM User u LEFT JOIN FETCH u.roles WHERE u.id = :id")
    Optional<User> findByIdWithRoles(@Param("id") Long id);
    
    /**
     * 根据用户名预加载角色查询用户
     */
    @Query("SELECT u FROM User u LEFT JOIN FETCH u.roles WHERE u.username = :username")
    Optional<User> findByUsernameWithRoles(@Param("username") String username);
    
    /**
     * 根据状态统计用户数量
     */
    long countByStatus(Integer status);
} 